//封装axios

import axios from "axios";
import { message } from "ant-design-vue";
import router from "../router/index";
import baseUrl from "./baseUrl";
axios.defaults.baseURL = baseUrl;
axios.defaults.withCredentials = true;
axios.defaults.crossDomain = true;

const msg = message;
msg.destroy();
//设置token 请求拦截器
axios.interceptors.request.use(
  config => {
    // 获取token
    const token = sessionStorage.getItem("tk");
    // 设置参数格式

    if (!config.headers["Content-Type"]) {
      config.headers = {
        "Content-Type": "application/json",
        credentials: "include"
      };
    }
    // 添加token到headers
    if (token) {
      config.headers.token = token;
    }
    // 鉴权参数设置
    if (config.method === "get") {
      //get请求下 参数在params中，其他请求在data中
      config.params = config.params || {};
      // let json = JSON.parse(JSON.stringify(config.params));
      //一些参数处理
    } else {
      config.data = config.data || {};
      //一些参数处理
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
// 响应拦截器
axios.interceptors.response.use(
  response => {
    let { code, message } = response.data;
    if (code !== undefined && message !== "") {
      if (code === "10000") {
        msg.destroy();
        msg.success(message);
      } else if (code === "204" || code === "10008") {
        msg.destroy();
        msg.error(message);
        sessionStorage.clear();
        router.push("/login");
      } else if (code === "10002") {
        console.log(code);
        // msg.error('必填项缺失！请检测必填项是否完整！')
      } else if (code === "10012") {
        console.log(code);
      } else if (code === "10003") {
        console.log(code);
      } else {
        msg.destroy();
        msg.error(message);
      }
    }
    return response;
  },
  error => {
    return Promise.reject(error);
  }
);
export default {
  get(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .get(url, { params })
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  },
  post(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, params)
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  },
  put(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .put(url, params)
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  },
  delete(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .delete(url, { params })
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  },
  blob(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, params, {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded" //请求的数据类型为form data格式
          },
          responseType: "blob"
        })
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  },
  formData(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, params, {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded" //请求的数据类型为form data格式
          },
        })
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  },
  blobJson(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, params, {
          responseType: "blob"
        })
        .then(res => {
          resolve(res.data);
        })
        .catch(erro => {
          reject(erro);
        });
    });
  }
};
